﻿using JshRCSWebApi.Infrastructure.Domain.DTO.SysVehicle.VehicleState;
using JshRCSWebApi.Infrastructure.Domain.DTO.SysVehicle;
using JshRCSWebApi.Infrastructure;
using JshRCSWebApi.Util.VehicleTool;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using JshRCSWebApi.Infrastructure.Domain.DTO.SysVehicle.VehicleTask;
using JshRCSWebApi.Service.SysVehicle;
using JshRCSWebApi.Util;
using JshRCSWebApi.Infrastructure.Domain.VO;
using JshRCSWebApi.Infrastructure.Domain.VO.SysVehicle.VehicleTask;

namespace JshRCSWebApi.ServiceImpl.SysVehicle
{
    public class VehicleTaskServiceImpl: VehicleTaskService
    {
        private int vehiclePort = 19206;

        /// <summary>
        /// 路径导航 3051           
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_task_gotarget_req(RequestDTO<robot_task_gotarget_DTO> requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 3051;
            request.askJson = JsonConvert.SerializeObject(requestDTO.askJson);
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<ResponseVO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 高级路径导航 3051           
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_task_gotarget_senior_req(RequestDTO<robot_task_gotarget_senior_DTO> requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 3051;
            request.askJson = JsonConvert.SerializeObject(requestDTO.askJson);
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<ResponseVO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 指定路径导航 3066            
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_task_gotargetlist_req(RequestDTO<robot_task_gotargetlist_DTO> requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 3066;
            request.askJson = JsonConvert.SerializeObject(requestDTO.askJson);
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<ResponseVO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 平动 3055             
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_task_translate_req(RequestDTO<robot_task_translate_DTO> requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 3055;
            request.askJson = JsonConvert.SerializeObject(requestDTO.askJson);
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<ResponseVO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 转动 3056              
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_task_turn_req(RequestDTO<robot_task_turn_DTO> requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 3056;
            request.askJson = JsonConvert.SerializeObject(requestDTO.askJson);
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<ResponseVO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 圆弧运动 3058               
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_task_circular_req(RequestDTO<robot_task_circular_DTO> requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 3058;
            request.askJson = JsonConvert.SerializeObject(requestDTO.askJson);
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<ResponseVO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 启用和禁用线路 3059                
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_task_path_req(RequestDTO<robot_task_path_DTO> requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 3059;
            request.askJson = JsonConvert.SerializeObject(requestDTO.askJson);
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<ResponseVO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 托盘旋转 3057                 
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_task_spin_req(RequestDTO<robot_task_spin_DTO> requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 3057;
            request.askJson = JsonConvert.SerializeObject(requestDTO.askJson);
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<ResponseVO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 暂停当前导航 3001                  
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_task_pause_req(RequestDTO  requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 3001;
            request.askJson = "";
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<ResponseVO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 继续当前导航 3002                   
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_task_resume_req(RequestDTO requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 3002;
            request.askJson = "";
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<ResponseVO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 取消当前导航 3003                    
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_task_cancel_req(RequestDTO requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 3003;
            request.askJson = "";
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<ResponseVO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 查询机器人任务链 3101                  
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_tasklist_status_req(RequestDTO<robot_tasklist_status_DTO> requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 3101;
            request.askJson = JsonConvert.SerializeObject(requestDTO.askJson);
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<robot_tasklist_status_VO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 查询机器人所有任务链 3115                     
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_tasklist_list_req(RequestDTO requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 3115;
            request.askJson = "";
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<robot_tasklist_list_VO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 执行预存任务链 3106                   
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_tasklist_name_req(RequestDTO<robot_tasklist_name_DTO> requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 3106;
            request.askJson = JsonConvert.SerializeObject(requestDTO.askJson);
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<ResponseVO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 获取路径导航的路径 3053                    
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_task_target_path_req(RequestDTO<robot_task_target_path_DTO> requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 3053;
            request.askJson = JsonConvert.SerializeObject(requestDTO.askJson);
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<robot_task_target_path_VO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 根据任务id清除指定导航路径 3068                     
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_task_safeclearmovements_req(RequestDTO<robot_task_safeclearmovements_DTO> requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 3068;
            request.askJson = JsonConvert.SerializeObject(requestDTO.askJson);
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<ResponseVO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }
    }
}
